package com.shms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfLogin;
import com.shms.dao.ShLoginDao;
import com.shms.entity.ShLogin;
import com.shms.until.PageUntil;

/**
 * 文件名：ShLoginDaoImpl.java
 * 编写者：杨勇
 * 创建时间：2013-3-11
 * 简要描述：
 * 
 */
public class ShLoginDaoImpl extends HibernateDaoSupport implements ShLoginDao {
	@SuppressWarnings({ "finally", "unchecked" })
	public List<ShLogin> queryLoginByExample(ShLogin shLogin) {
		@SuppressWarnings("rawtypes")
		List list = new ArrayList();
		try {
			StringBuffer strBuf = new StringBuffer();
			//根据登录名和密码，查询登录数据
			strBuf.append("SELECT L.*");
			strBuf.append(" FROM SH_LOGIN L");
			strBuf.append(" WHERE L.LNAME = :lname AND L.LPASS = :lpass");
			
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(strBuf.toString());
			//为SQL语句绑定参数
			sqlQuery.setProperties(shLogin);
			
			list = sqlQuery.addEntity(ShLogin.class).list();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return list;
		}
	}
	
	@SuppressWarnings("finally")
	public GroupOfLogin queryLogin(GroupOfLogin groupOfLogin) {
		GroupOfLogin gOfLoginRtn = new GroupOfLogin();
		try {
			//查询数据总条数的SQL语句
			String sql = "SELECT COUNT(*) FROM SH_LOGIN";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfLogin.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfLogin.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfLogin.getPageSize(), currentPage);
			
			sql = "SELECT * FROM SH_LOGIN ORDER BY LNAME DESC";
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfLogin.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShLogin> loginListRtn = sqlQuery.addEntity(ShLogin.class).list();
			gOfLoginRtn = new GroupOfLogin(currentPage, totalPage, allRow, loginListRtn); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfLoginRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			String sql = "DELETE FROM SH_LOGIN L WHERE L.LID IN (:ids)";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean saveLogin(ShLogin shLogin) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shLogin);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean updateLogin(ShLogin shLogin) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shLogin);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public ShLogin queryById(ShLogin shLogin) {
		ShLogin shLoginRtn = new ShLogin();
		try {
			String sql = "SELECT * FROM SH_LOGIN WHERE LID = :lid";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shLogin);
			@SuppressWarnings("unchecked")
			List<ShLogin> listLogin = sqlQuery.addEntity(ShLogin.class).list();
			if (listLogin.size() == 1) {
				shLoginRtn = listLogin.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shLoginRtn;
		}
	}
}
